﻿using System;
using System.Diagnostics;
using System.Net;

namespace HslCommunication.Profinet.Knx
{
	/// <summary>
	/// Knx协议
	/// </summary>
	// Token: 0x02000087 RID: 135
	public class KnxCode
	{
		/// <summary>
		/// 返回需要写入KNX总线的应答报文（应答数据）
		/// </summary>
		// Token: 0x1400000B RID: 11
		// (add) Token: 0x06000A84 RID: 2692 RVA: 0x0003F640 File Offset: 0x0003D840
		// (remove) Token: 0x06000A85 RID: 2693 RVA: 0x0003F678 File Offset: 0x0003D878
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event KnxCode.ReturnData Return_data_msg;

		/// <summary>
		/// 返回需要写入的KNX系统的报文（写入数据）
		/// </summary>
		// Token: 0x1400000C RID: 12
		// (add) Token: 0x06000A86 RID: 2694 RVA: 0x0003F6B0 File Offset: 0x0003D8B0
		// (remove) Token: 0x06000A87 RID: 2695 RVA: 0x0003F6E8 File Offset: 0x0003D8E8
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event KnxCode.ReturnData Set_knx_data;

		/// <summary>
		/// 返回从knx系统得到的数据
		/// </summary>
		// Token: 0x1400000D RID: 13
		// (add) Token: 0x06000A88 RID: 2696 RVA: 0x0003F720 File Offset: 0x0003D920
		// (remove) Token: 0x06000A89 RID: 2697 RVA: 0x0003F758 File Offset: 0x0003D958
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event KnxCode.GetData GetData_msg;

		/// <summary>
		/// 序号计数
		/// </summary>
		// Token: 0x170001B6 RID: 438
		// (get) Token: 0x06000A8A RID: 2698 RVA: 0x0003F78D File Offset: 0x0003D98D
		// (set) Token: 0x06000A8B RID: 2699 RVA: 0x0003F795 File Offset: 0x0003D995
		public byte SequenceCounter { get; set; }

		/// <summary>
		/// 通道
		/// </summary>
		// Token: 0x170001B7 RID: 439
		// (get) Token: 0x06000A8C RID: 2700 RVA: 0x0003F79E File Offset: 0x0003D99E
		// (set) Token: 0x06000A8D RID: 2701 RVA: 0x0003F7A6 File Offset: 0x0003D9A6
		public byte Channel { get; set; }

		/// <summary>
		/// 连接状态
		/// </summary>
		// Token: 0x170001B8 RID: 440
		// (get) Token: 0x06000A8E RID: 2702 RVA: 0x0003F7AF File Offset: 0x0003D9AF
		// (set) Token: 0x06000A8F RID: 2703 RVA: 0x0003F7B7 File Offset: 0x0003D9B7
		public bool IsConnect { get; private set; }

		/// <summary>
		/// 关闭KNX连接
		/// </summary>
		/// <param name="channel">通道号</param>
		/// <param name="IP_PROT">本机IP</param>
		/// <returns></returns>
		// Token: 0x06000A90 RID: 2704 RVA: 0x0003F7C0 File Offset: 0x0003D9C0
		public byte[] Disconnect_knx(byte channel, IPEndPoint IP_PROT)
		{
			byte[] addressBytes = IP_PROT.Address.GetAddressBytes();
			byte[] bytes = BitConverter.GetBytes(IP_PROT.Port);
			return new byte[]
			{
				6,
				16,
				2,
				9,
				0,
				16,
				channel,
				0,
				8,
				1,
				addressBytes[0],
				addressBytes[1],
				addressBytes[2],
				addressBytes[3],
				bytes[1],
				bytes[0]
			};
		}

		/// <summary>
		/// 返回握手报文
		/// </summary>
		/// <param name="IP_PROT">本机ip地址</param>
		/// <returns></returns>
		// Token: 0x06000A91 RID: 2705 RVA: 0x0003F84C File Offset: 0x0003DA4C
		public byte[] Handshake(IPEndPoint IP_PROT)
		{
			byte[] addressBytes = IP_PROT.Address.GetAddressBytes();
			byte[] bytes = BitConverter.GetBytes(IP_PROT.Port);
			return new byte[]
			{
				6,
				16,
				2,
				5,
				0,
				26,
				8,
				1,
				addressBytes[0],
				addressBytes[1],
				addressBytes[2],
				addressBytes[3],
				bytes[1],
				bytes[0],
				8,
				1,
				addressBytes[0],
				addressBytes[1],
				addressBytes[2],
				addressBytes[3],
				bytes[1],
				bytes[0],
				4,
				4,
				2,
				0
			};
		}

		/// <summary>
		/// KNX报文解析
		/// </summary>
		/// <param name="in_data"></param>
		// Token: 0x06000A92 RID: 2706 RVA: 0x0003F914 File Offset: 0x0003DB14
		public void KNX_check(byte[] in_data)
		{
			byte b = in_data[2];
			byte b2 = b;
			if (b2 != 2)
			{
				if (b2 == 4)
				{
					this.KNX_serverOF_4(in_data);
				}
			}
			else
			{
				this.KNX_serverOF_2(in_data);
			}
		}

		/// <summary>
		/// 写入数据到KNX系统
		/// </summary>
		/// <param name="addr">地址</param>
		/// <param name="len">长度</param>
		/// <param name="data">数据</param>
		// Token: 0x06000A93 RID: 2707 RVA: 0x0003F94C File Offset: 0x0003DB4C
		public void Knx_Write(short addr, byte len, byte[] data)
		{
			byte[] bytes = BitConverter.GetBytes(addr);
			byte[] array = new byte[(int)(20 + len)];
			byte[] bytes2 = BitConverter.GetBytes(array.Length);
			bool flag = this.SequenceCounter + 1 <= byte.MaxValue;
			if (flag)
			{
				bool flag2 = this.is_fresh;
				if (flag2)
				{
					byte sequenceCounter = this.SequenceCounter + 1;
					this.SequenceCounter = sequenceCounter;
				}
				else
				{
					this.is_fresh = true;
				}
			}
			else
			{
				this.SequenceCounter = 0;
			}
			array[0] = 6;
			array[1] = 16;
			array[2] = 4;
			array[3] = 32;
			array[4] = bytes2[1];
			array[5] = bytes2[0];
			array[6] = 4;
			array[7] = this.Channel;
			array[8] = this.SequenceCounter;
			array[9] = 0;
			array[10] = 17;
			array[11] = 0;
			array[12] = 188;
			array[13] = 224;
			array[14] = 0;
			array[15] = 0;
			array[16] = bytes[1];
			array[17] = bytes[0];
			array[18] = len;
			array[19] = 0;
			bool flag3 = len == 1;
			if (flag3)
			{
				byte[] bytes3 = BitConverter.GetBytes((int)((data[0] & 63) | 128));
				array[20] = bytes3[0];
			}
			else
			{
				array[20] = 128;
				for (int i = 2; i <= (int)len; i++)
				{
					array[(int)(len - 1 + 20)] = data[i - 2];
				}
			}
			bool flag4 = this.Set_knx_data != null;
			if (flag4)
			{
				this.Set_knx_data(array);
			}
		}

		/// <summary>
		/// 从KNX获取数据
		/// </summary>
		/// <param name="addr"></param>
		// Token: 0x06000A94 RID: 2708 RVA: 0x0003FAB4 File Offset: 0x0003DCB4
		public void Knx_Resd_step1(short addr)
		{
			byte[] bytes = BitConverter.GetBytes(addr);
			byte[] array = new byte[21];
			byte[] bytes2 = BitConverter.GetBytes(array.Length);
			bool flag = this.SequenceCounter + 1 <= byte.MaxValue;
			if (flag)
			{
				bool flag2 = this.is_fresh;
				if (flag2)
				{
					byte sequenceCounter = this.SequenceCounter + 1;
					this.SequenceCounter = sequenceCounter;
				}
				else
				{
					this.is_fresh = true;
				}
			}
			else
			{
				this.SequenceCounter = 0;
			}
			array[0] = 6;
			array[1] = 16;
			array[2] = 4;
			array[3] = 32;
			array[4] = bytes2[1];
			array[5] = bytes2[0];
			array[6] = 4;
			array[7] = this.Channel;
			array[8] = this.SequenceCounter;
			array[9] = 0;
			array[10] = 17;
			array[11] = 0;
			array[12] = 188;
			array[13] = 224;
			array[14] = 0;
			array[15] = 0;
			array[16] = bytes[1];
			array[17] = bytes[0];
			array[18] = 1;
			array[19] = 0;
			array[20] = 0;
			bool flag3 = this.Set_knx_data != null;
			if (flag3)
			{
				this.Return_data_msg(array);
			}
		}

		/// <summary>
		/// 连接保持（每隔1s发送一次到设备）
		/// </summary>
		/// <param name="IP_PROT"></param>
		// Token: 0x06000A95 RID: 2709 RVA: 0x0003FBC4 File Offset: 0x0003DDC4
		public void knx_server_is_real(IPEndPoint IP_PROT)
		{
			byte[] array = new byte[16];
			byte[] addressBytes = IP_PROT.Address.GetAddressBytes();
			byte[] bytes = BitConverter.GetBytes(IP_PROT.Port);
			array[0] = 6;
			array[1] = 16;
			array[2] = 2;
			array[3] = 7;
			array[4] = 0;
			array[5] = 16;
			array[6] = this.Channel;
			array[7] = 0;
			array[8] = 8;
			array[9] = 1;
			array[10] = addressBytes[0];
			array[11] = addressBytes[1];
			array[12] = addressBytes[2];
			array[13] = addressBytes[3];
			array[14] = bytes[1];
			array[15] = bytes[0];
			bool flag = this.Return_data_msg != null;
			if (flag)
			{
				this.Return_data_msg(array);
			}
		}

		/// <summary>
		/// 暂时没有注释
		/// </summary>
		/// <param name="addr"></param>
		/// <param name="is_ok"></param>
		/// <returns></returns>
		// Token: 0x06000A96 RID: 2710 RVA: 0x0003FC68 File Offset: 0x0003DE68
		public short Get_knx_addr(string addr, out bool is_ok)
		{
			short num = 0;
			string[] array = addr.Split(new char[]
			{
				'\\'
			});
			bool flag = array.Length == 3;
			short result;
			if (flag)
			{
				int num2 = int.Parse(array[0]);
				int num3 = int.Parse(array[1]);
				int num4 = int.Parse(array[2]);
				bool flag2 = num2 > 31 || num3 > 7 || num4 > 255 || num2 < 0 || num3 < 0 || num4 < 0;
				if (flag2)
				{
					Console.WriteLine("地址不合法");
					is_ok = false;
					result = num;
				}
				else
				{
					num2 <<= 11;
					num3 <<= 8;
					int num5 = num2 | num3 | num4;
					num = (short)num5;
					is_ok = true;
					result = num;
				}
			}
			else
			{
				Console.WriteLine("地址不合法");
				is_ok = false;
				result = num;
			}
			return result;
		}

		// Token: 0x06000A97 RID: 2711 RVA: 0x0003FD30 File Offset: 0x0003DF30
		private void KNX_serverOF_2(byte[] in_data)
		{
			switch (in_data[3])
			{
			case 6:
				this.Extraction_of_Channel(in_data);
				break;
			case 7:
				this.Return_status();
				break;
			}
		}

		/// <summary>
		/// 返回连接状态
		/// </summary>
		// Token: 0x06000A98 RID: 2712 RVA: 0x0003FD78 File Offset: 0x0003DF78
		private void Return_status()
		{
			byte[] data = new byte[]
			{
				6,
				16,
				2,
				8,
				0,
				8,
				this.Channel,
				0
			};
			bool flag = this.Return_data_msg != null;
			if (flag)
			{
				this.Return_data_msg(data);
			}
		}

		/// <summary>
		/// 从握手回复报文获取通道号
		/// </summary>
		/// <param name="in_data"></param>
		// Token: 0x06000A99 RID: 2713 RVA: 0x0003FDD0 File Offset: 0x0003DFD0
		private void Extraction_of_Channel(byte[] in_data)
		{
			this.Channel = in_data[6];
			bool flag = in_data[5] == 8 & in_data[7] == 37;
			if (flag)
			{
				this.IsConnect = false;
			}
			bool flag2 = this.Channel > 0;
			if (flag2)
			{
				this.IsConnect = true;
			}
		}

		// Token: 0x06000A9A RID: 2714 RVA: 0x0003FE1C File Offset: 0x0003E01C
		private void KNX_serverOF_4(byte[] in_data)
		{
			byte b = in_data[3];
			byte b2 = b;
			if (b2 != 32)
			{
				if (b2 != 33)
				{
				}
			}
			else
			{
				this.Read_com_CEMI(in_data);
			}
		}

		/// <summary>
		/// 解析控制包头和CEMI
		/// </summary>
		// Token: 0x06000A9B RID: 2715 RVA: 0x0003FE4A File Offset: 0x0003E04A
		private void Read_com_CEMI(byte[] in_data)
		{
			this.Read_CEMI(in_data);
		}

		/// <summary>
		///             具体解析CEMI 
		/// </summary>
		// Token: 0x06000A9C RID: 2716 RVA: 0x0003FE58 File Offset: 0x0003E058
		private void Read_CEMI(byte[] in_data)
		{
			bool flag = in_data.Length > 11;
			if (flag)
			{
				byte b = in_data[10];
				byte b2 = b;
				if (b2 != 17)
				{
					if (b2 != 41)
					{
						if (b2 == 46)
						{
							this.Read_CEMI_2e(in_data);
						}
					}
					else
					{
						this.Read_CEMI_29(in_data);
					}
				}
			}
		}

		// Token: 0x06000A9D RID: 2717 RVA: 0x0003FEA8 File Offset: 0x0003E0A8
		private void Read_CEMI_2e(byte[] in_data)
		{
			byte[] data = new byte[]
			{
				6,
				16,
				4,
				33,
				0,
				10,
				4,
				this.Channel,
				in_data[8],
				0
			};
			bool flag = this.Set_knx_data != null;
			if (flag)
			{
				this.Return_data_msg(data);
			}
		}

		// Token: 0x06000A9E RID: 2718 RVA: 0x0003FF10 File Offset: 0x0003E110
		private void Read_CEMI_29(byte[] in_data)
		{
			short addr = BitConverter.ToInt16(new byte[]
			{
				in_data[17],
				in_data[16]
			}, 0);
			bool flag = in_data[18] > 1;
			byte[] array;
			if (flag)
			{
				array = new byte[(int)in_data[17]];
				for (int i = 0; i < (int)(in_data[18] - 1); i++)
				{
					array[i] = in_data[21 + i];
				}
			}
			else
			{
				array = BitConverter.GetBytes((int)(in_data[20] & 63));
			}
			bool flag2 = this.GetData_msg != null;
			if (flag2)
			{
				this.GetData_msg(addr, in_data[18], array);
			}
			this.Read_setp6(in_data);
		}

		// Token: 0x06000A9F RID: 2719 RVA: 0x0003FFB8 File Offset: 0x0003E1B8
		private void Read_setp6(byte[] in_data)
		{
			byte[] data = new byte[]
			{
				6,
				16,
				4,
				33,
				0,
				10,
				4,
				this.Channel,
				in_data[8],
				0
			};
			bool flag = this.Return_data_msg != null;
			if (flag)
			{
				this.Return_data_msg(data);
			}
		}

		// Token: 0x04000307 RID: 775
		private bool is_fresh = false;

		/// <summary>
		/// 返回数据的委托
		/// </summary>
		/// <param name="data"></param>
		// Token: 0x02000391 RID: 913
		// (Invoke) Token: 0x06002750 RID: 10064
		public delegate void ReturnData(byte[] data);

		/// <summary>
		/// 获取数据的委托
		/// </summary>
		/// <param name="addr"></param>
		/// <param name="len"></param>
		/// <param name="data"></param>
		// Token: 0x02000392 RID: 914
		// (Invoke) Token: 0x06002754 RID: 10068
		public delegate void GetData(short addr, byte len, byte[] data);
	}
}
